package com.huawei.tool.application.service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

import org.apache.logging.log4j.core.util.CronExpression;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

/**
 * 功能描述
 *
 * @author lWX1200568
 * @since 2024-07-26
 */
@Configuration
@EnableScheduling
public class DemoCron implements SchedulingConfigurer {
    private String cron = "0 0/1 * * * ? ";

    // 修改 实际执行时间
    private String finalCorn = "0 1,3,5,7,11,14,17,19,21,24,27,29,31,33,35,38,41,47,51,55 * * * ? ";

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        Runnable task = new Runnable() {
            @Override
            public void run() {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date now = new Date();
                System.out.println("当前时间减一分钟的= " + now.toString());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(now);
                // 减去一分钟
                calendar.add(Calendar.MINUTE, -1);
                // 转换为Date对象
                Date newDate = calendar.getTime();
                System.out.println("当前时间减一分钟的= " + newDate.toString());
                Date prevFireTime = null;
                Date nextInvalidTimeAfter = null;
                try {
                    CronExpression cronExpression = new CronExpression(finalCorn);
                    nextInvalidTimeAfter = cronExpression.getNextValidTimeAfter(newDate);
                    if (nextInvalidTimeAfter != null) {
                        System.out.println("-------getNextValidTimeAfter-----" + sdf.format(nextInvalidTimeAfter));
                    }
                    Date timeAfter = cronExpression.getTimeAfter(newDate);
                    if (timeAfter != null) {
                        System.out.println("-------timeAfter-----" + sdf.format(timeAfter));
                    }
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }

                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String format = sf.format(nextInvalidTimeAfter);
                String format1 = sf.format(now);
                System.out.println(format + " " + format1);
                // 根据时间是否和动态cron匹配去判断执行业务
                if (format1.equals(format)){
                    System.out.println("doing----------" + LocalDateTime.now().toString());
                    System.out.println("done----------");
                }

            }
        };

        Trigger trigger = new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {

                //执行每分钟去执行
                CronTrigger cronTrigger = new CronTrigger(cron);
                Date nextExecTime = cronTrigger.nextExecutionTime(triggerContext);
                System.out.println("--next = "+nextExecTime.toLocaleString()+"--------nextExecutionTime----------cur=-" + LocalDateTime.now().toString());
                return nextExecTime;
            }
        };
        taskRegistrar.addTriggerTask(task, trigger);
    }
}
